import sys
import random

''' 从标准输入获取一个数字'''


def get_input_num(min=1, max=100):
    print("请输入一个数字:", end="")
    while True:
        input_ = sys.stdin.readline().strip()
        if not input_.isdigit():
            print("输入数字无效,请重新输入:", end="")
        elif int(input_) > max or int(input_) < min:
            print("超过输入范围，请重新输入:", end="")
        else:
            break
    # 返回数字
    return int(input_)


def guess_number():
    print("猜数游戏开始")
    num = get_input_num()
    answer = random.randint(1, 100)

    while True:
        if num < answer:
            print("小了")
            num = get_input_num()
        elif num > answer:
            print("大了")
            num = get_input_num()
        else:
            print("猜对了")
            break


if __name__ == '__main__':
    guess_number()
